<?php
class NPageNav {
	function __construct($total_items, $items_perpage, $current_start,$start_name="start"){
	$this->total = intval($total_items);
        $this->perpage = intval($items_perpage);
        $this->current = intval($current_start);
        $this->uri = NURI::getInstance();
	

	}
 function &getInstance($total_items, $items_perpage, $current_start,$start_name="start"){
	return new NPageNav($total_items,$items_perpage,$current_start,$start_name);
 }
 function renderNav($offset = 4)
    {
        $ret = '';
        if ( $this->total <= $this->perpage ) {
            return $ret;
        }
        $total_pages = ceil($this->total / $this->perpage);
        if ( $total_pages > 1 ) {
            $prev = $this->current - $this->perpage;
            if ( $prev >= 0 ) {
            
			$this->uri->setVar('start',$prev);
		$ret .= '<a href="'.$this->uri->toString().'">&laquo;</a> ';
			$this->uri->delVar('start');
            }
            $counter = 1;
            $current_page = intval(floor(($this->current + $this->perpage) / $this->perpage));
            while ( $counter <= $total_pages ) {
                if ( $counter == $current_page ) {
                    $ret .= '<b>('.$counter.')</b> ';
                } elseif ( ($counter > $current_page-$offset && $counter < $current_page + $offset ) || $counter == 1 || $counter == $total_pages ) {
                    if ( $counter == $total_pages && $current_page < $total_pages - $offset ) {
                        $ret .= '... ';
                    }
			$this->uri->setVar('start',(($counter - 1) * $this->perpage));
		   $ret .= '<a href="'.$this->uri->toString().'">'.$counter.'</a> ';
			$this->uri->delVar('start');
                    if ( $counter == 1 && $current_page > 1 + $offset ) {
                        $ret .= '... ';
                    }
                }
                $counter++;
            }
            $next = $this->current + $this->perpage;
            if ( $this->total > $next ) {
			$this->uri->setVar('start',$next);
		$ret .= '<a href="'.$this->uri->toString().'">&raquo;</a> ';
			$this->uri->delVar('start');

            }
        }
        return $ret;
    }
}
?>